﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ProgressBars
{
    /// <summary>
    /// Handle to a progress bar - for thread safe manipulations
    /// </summary>
    public class ProgBarsHdl
    {
        delegate void UIEmptyThreadDelegate();
        delegate void UIParameterThreadDelegate(object obj);

        ProgressBar prgBarRef;
        /// <summary>
        /// Name of the progress bar object.
        /// </summary>
        public string ProgBarText;

        /// <summary>
        /// Public constructor
        /// </summary>
        /// <param name="name">Name of the progress bar</param>
        /// <param name="iPrgBarRef">Windows forms progress bar reference</param>
        public ProgBarsHdl(string name, ProgressBar iPrgBarRef)
        {
            ProgBarText = name;
            prgBarRef = iPrgBarRef;
        }

        #region Thread safe UI update calls

        public void SetMaximum(int max)
        {
            prgBarRef.Invoke(new UIParameterThreadDelegate(_setMaximum), new object[] { max });
        }

        public void SetMinimum(int min)
        {
            prgBarRef.Invoke(new UIParameterThreadDelegate(_setMinimum), new object[] { min });
        }

        public void SetStep(int step)
        {
            prgBarRef.Invoke(new UIParameterThreadDelegate(_setStep), new object[] { step });
        }

        public void Step()
        {
            prgBarRef.Invoke(new UIEmptyThreadDelegate(_step));
        }

        public void SetForeColour(Color clr)
        {
            prgBarRef.Invoke(new UIParameterThreadDelegate(_setForeColour), new object[] { clr });
        }

        public void SetStyle(ProgressBarStyle style)
        {
            prgBarRef.Invoke(new UIParameterThreadDelegate(_setStyle), new object[] { style });
        }

        #endregion

        #region actual UI element updating

        void _setMaximum(object obj)
        {
            prgBarRef.Maximum = (int)obj;
        }

        void _setMinimum(object obj)
        {
            prgBarRef.Minimum = (int)obj;
        }

        void _setStep(object obj)
        {
            prgBarRef.Step = (int)obj;
        }

        void _step()
        {
            prgBarRef.PerformStep();
        }

        void _setForeColour(object obj)
        {
            prgBarRef.ForeColor = (Color)obj;
        }

        void _setStyle(object obj)
        {
            prgBarRef.Style = (ProgressBarStyle)obj;
        }

        #endregion
    }
}
